import { Request,Response } from "express";
import { delay } from '@/utils/MockUtils';

export const gms = [
    {
        'gm_id':0,
        'username':'root',
        'password':'c8a1ddb104924015f9bf52b645739615',
        'create_time':1550160000000,
        'deleted':0
    },
    {
        'gm_id':1,
        'username':'lily',
        'password':'c8a1ddb104924015f9bf52b645739615',
        'create_time':1550160000000,
        'deleted':0
    },
    {
        'gm_id':2,
        'username':'xiaoli',
        'password':'',
        'create_time':1550160000000,
        'deleted':0
    }
];
const proxy = {
    'get /api/v1/gm/query':function(req:Request,resp:Response){
        resp.setHeader('Access-Control-Allow-Origin', '*');

        resp.json({
            'error_code': 0,
            'data': gms,
            'pagination':{
                'current':1,
                'page_size':10,
                'total':gms.length
            }
        });
    },
    'post /api/v1/gm/delete_by_id/:id':function(req:Request,resp:Response){
        resp.setHeader('Access-Control-Allow-Origin', '*');

        const id = req.params.id;
        // console.log('要删除的数据', id);
        //根据索引删除指定的数据.
        const index = gms.findIndex(elem => {
            return elem.gm_id === parseInt(id);
        });
        // console.log('要删除的数据索引:', index);
        if(index >= 0) {
            gms.splice(index, 1);
            resp.json({
                'error_code': 0,
                'data':1
            });
        }else{
            resp.json({
                'error_code': -1,
                'data':0
            });
        }
    },
    'post /api/v1/gm/add':function(req:Request,resp:Response){
        resp.setHeader('Access-Control-Allow-Origin', '*');
        setTimeout(()=>{},3000);//模拟延迟

        const item = req.body;
        item.gm_id = gms.length + 1;
        gms.push(item);
        resp.json({
            'error_code': 0,
            'data':1
        });
    },

    'post /api/v1/gm/edit_passwd/:id':function(req:Request,resp:Response){
        resp.setHeader('Access-Control-Allow-Origin', '*');

        const id = req.params.id;
        const item = req.body;
        // console.log('待修改的id', id, '新密码', item);

        resp.json({
            'error_code': 0,
            'data':1
        });
    }
}

export default delay(proxy,2000);//模拟延迟
